package repositorios;

import entidades.Setor;
import excessoes.DAOException;
import interfaces.InterfaceSetorDAO;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.exception.ConstraintViolationException;

/**
 *
 * @author Marcelo Gomes Neto
 */
public class SetorDAOHibernate implements InterfaceSetorDAO {
    
    private Session session;

    public void setSessao(org.hibernate.Session session) {
        this.session = session;
    }

    @Override
    public Setor carregarSetor(Integer id) {
        return (Setor) this.session.get(Setor.class, id);
    }
        
    @Override
    public void alterarSetor(Setor setor) 
    {
        Integer id = setor.getIdsetor();
        if (id == null || id == 0)
        {
            Setor setorId = this.carregarSetor(id);
            setorId.setIdsetor(id);
            this.session.evict(id);
        }
        this.session.clear();
        this.session.update(setor);
    }

    @Override
    public void excluirSetor(Setor setor) {
        this.session.delete(setor);
    }

    @Override
    public void salvarSetor(Setor setor) throws DAOException 
    {
        try {
           this.session.save(setor);
        }
        catch (ConstraintViolationException e) {
            throw new DAOException("Erro de chave duplicada\n" + e.getConstraintName());
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Setor> listarSetor() 
    {
        Criteria criteria = this.session.createCriteria(Setor.class);
        criteria.addOrder((Order.asc("idsetor")));
        return criteria.list();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<Setor> listarSetorDescricao() 
    {
        Criteria criteria = this.session.createCriteria(Setor.class);
        criteria.addOrder((Order.asc("descricaoSetor")));
        return criteria.list();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<Setor> listarSiglaSetor() 
    {
        Criteria criteria = this.session.createCriteria(Setor.class);
        criteria.addOrder((Order.asc("sigla")));
        return criteria.list();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<Setor> listarSetorPorOM(int idunidade)
    {
        Criteria criteria = session.createCriteria(Setor.class);
        criteria.add(Restrictions.eq("idunidade", idunidade));
        criteria.addOrder((Order.asc("siglaSetor")));
        //this.session.clear();
        return criteria.list();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public Setor carregaSetorPorOM(int idunidade, int idsetor)
    {
        Criteria criteria = session.createCriteria(Setor.class);
        criteria.add(Restrictions.eq("idunidade", idunidade));
        criteria.add(Restrictions.eq("idsetor", idsetor));
        //this.session.clear();
        return (Setor) criteria.uniqueResult();
    }
    
    /*
    @Override
    @SuppressWarnings("unchecked")
    public List<String> listarTipo() 
    {
        Criteria criteria = this.session.createCriteria(Setor.class);
        criteria.addOrder((Order.asc("idsetor")));
        return criteria.list();
    }
    */
}
